package com.GlobalPaint.app.ui.location;


import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.utils.UiUtil;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapViewLayoutParams;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;


public class MyLocationFragmentNew extends Fragment {
    private LocationManager locationManager;
    private TextureMapView map_view = null;
    private BaiduMap mBaiduMap2;
    private LocationClient mLocationClient2;
    private BDLocationListener mBDLocationListener2;
    private View popView2;
    LocationClientOption option2;
    private TextView tv_title;
    private LatLng dxu;
    private Button bt_fuwei;
    private static final String TAG = "MyLocationFragment";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_location, container, false);


        popView2 = View.inflate(getContext(), R.layout.pop_view1, null);
        bt_fuwei = (Button)view. findViewById(R.id.bt_fuwei);
        bt_fuwei.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 启动定位
                mLocationClient2.start();
                mLocationClient2.restart();
                // 注册监听
                mLocationClient2.registerLocationListener(mBDLocationListener2);
            }
        });


        map_view = (TextureMapView) view.findViewById(R.id.map_view);
        tv_title = (TextView) popView2.findViewById(R.id.tv_title);
        //获取baidumap对象
        mBaiduMap2 = map_view.getMap();
        // 设置可改变地图位置
        mBaiduMap2.setMyLocationEnabled(true);
        //回调 获取坐标
        // 声明LocationClient类
        mLocationClient2 = new LocationClient(getContext());
        mBDLocationListener2 = new MyBDLocationListener2();
        // 声明定位参数
        option2 = new LocationClientOption();
        option2.setCoorType("bd09ll");// 设置返回定位结果是百度经纬度 默认gcj02
        option2.setIsNeedAddress(true);// 设置定位结果包含地址信息
        option2.setNeedDeviceDirect(true);// 设置定位结果包含手机机头 的方向
        // 设置定位参数
        mLocationClient2.setLocOption(option2);
        // 启动定位
        mLocationClient2.start();
        // 注册监听
        mLocationClient2.registerLocationListener(mBDLocationListener2);
        return view;
    }
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        super.onResume();
        map_view.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        map_view.onPause();
    }

    private void initView(double latitude, double longitude) {
        MarkerOptions options = new MarkerOptions();
        BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.qqq);
        options.position(new LatLng(latitude,longitude))
                .icon(icon)
                .draggable(true);
        mBaiduMap2.addOverlay(options);
    }
    private void initPopView(double latitude, double longitude, final String address) {
        popView2.setVisibility(View.GONE);

        MapStatusUpdate loc = MapStatusUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 15);
        mBaiduMap2.setMapStatus(loc);



        mBaiduMap2.setOnMapTouchListener(new BaiduMap.OnMapTouchListener() {
            /**
             * 当用户触摸地图时回调函数
             * @param event 触摸事件
             */
            public void onTouch(MotionEvent event) {
                mBaiduMap2.hideInfoWindow();//隐藏消息窗
                popView2.setVisibility(View.GONE);
            }
        });
        if (popView2.getParent() == null)
            map_view.addView(popView2, createLayoutParams(dxu));

        mBaiduMap2.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                 updatePopView(marker,address);
                return true;
            }
        });
    }
    private void updatePopView(final Marker marker, String address) {
        map_view.updateViewLayout(popView2, createLayoutParams(marker.getPosition()));
        popView2.setVisibility(View.VISIBLE);
        tv_title.setText(address);    // 显示标志的标题
    }

    private MapViewLayoutParams createLayoutParams(LatLng xzPos) {
        MapViewLayoutParams params = new MapViewLayoutParams.Builder()
                .layoutMode(MapViewLayoutParams.ELayoutMode.mapMode)    // 坐标类型为经纬度的类型
                .position(xzPos)    // 指定View的位置
                .yOffset(UiUtil.dp2px(-28))       // 指定View在y方向的偏移量
                .build();
        return params;
    }
    private class MyBDLocationListener2 implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            // 非空判断
            if (location != null) {
                mBaiduMap2.clear();
                // 根据BDLocation 对象获得经纬度以及详细地址信息
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                String address = location.getAddrStr();
                Log.i("12345678不同页面", "address:" + address + " latitude:" + latitude
                        + " longitude:" + longitude + "---");
                dxu = new LatLng(latitude, longitude);
               initView(latitude,longitude);
               initPopView(latitude,longitude,address);
            }
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mLocationClient2.stop();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        map_view.onDestroy();
    }
}
